package com.buba.springboot.redis;

import com.buba.springboot.utils.RandomUtils;
import redis.clients.jedis.Jedis;

import java.util.Random;

public class RedisYanZheng {
    public static void main(String[] args) {
        //verifyCode("123123123123");
        checkCode("23213","13473514887");
    }

    public static void verifyCode(String phone){
        //创建Jedis对象
        Jedis jedis=new Jedis("127.0.0.1",6379);
        //拼接key
        //手机验证码key拼接
        String countKey="verifyCode"+phone+":count";
        //验证码key
        String codeKey="verifyCode"+phone+":code";

        //每个手机每天只能发生三次
        String count = jedis.get(countKey);
        if(count == null){
            //没有发送次数，第一次发送
            //设置发送次数是1
            jedis.setex(countKey,24*60*60,"1");
        }else if(Integer.parseInt(count)<=2){
            //发送次数+1
            jedis.incr(codeKey);
        }else if(Integer.parseInt(count)>2){
            //发送三次，不能再次进行发送
            System.out.println("不能发送，今天的发送次数已经超过三次。");
            jedis.close();
        }
        //发短信到redis里面
        String vcode= RandomUtils.getSixCode();
        System.out.println(vcode);
        jedis.setex(codeKey,120,vcode);
        jedis.close();
    }
    //做验证码校验
    public static void checkCode(String code,String phone){
        Jedis jedis=new Jedis("127.0.0.1",6379);
        //验证码key
        String codeKey="verifyCode"+phone+":code";
        String s = jedis.get(codeKey);
        if(code.equals(s)){
            System.out.println("验证成功");
        }else {
            System.out.println("验证失败");
        }
    }

}

